﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace leviathan_mapeditor
{
    public enum mainState { init, ok, error };

    public class MainForm
    {
        public struct mission
        {
            public string desc;
            public string left;
            public string right;
            public string fight;
            public string search;
        }

        private mainState status;
        private string scenarioName = "";

        private List<mission> sce;
        private int actMission = 0;
        private int all = 0;

        public MainForm()
        {
            status = mainState.init;
            sce = new List<mission>();
        }

        public mainState getStatus()
        {
            return status;
        }

        public void newScenario(string inname)
        {
            scenarioName = inname;
            string path = "../../saves/" + scenarioName + ".sco";

            if (File.Exists(path))
            {
                System.Windows.Forms.MessageBox.Show("Megadott kalandnév már létezik!", "Hiba!");
                return;
            }

            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("[kezdet]");       //Szöveg
                sw.WriteLine("0");              //Left
                sw.WriteLine("0");              //Right
                sw.WriteLine("0");              //Fight
                sw.WriteLine("0");              //Search
                sw.Close();
            }
            using (StreamWriter sw = new StreamWriter("../../saves/SList.lst", true))
            {
                sw.WriteLine(scenarioName);
            }
            
            loadScenario(scenarioName);
        }

        public void loadScenario(string sname)
        {
            string path = "../../saves/" + sname + ".sco";

            scenarioName = sname;

            if (File.Exists(path))
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    while (!sr.EndOfStream)
                    {
                        mission seged;
                        seged.desc = sr.ReadLine();                     //Szöveg
                        seged.left = sr.ReadLine();                     //Left
                        seged.right = sr.ReadLine();                    //Right
                        seged.fight = sr.ReadLine();                    //Fight
                        seged.search = sr.ReadLine();                   //Search
                        sce.Add(seged);
                        ++all;
                    }
                    sr.Close();
                }
                actMission = 0;
                status = mainState.ok;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Megadott kaland nem létezik!", "Hiba!");
                status = mainState.error;
                return;
            }
        }

        public void saveScenario()
        {
            string path = "../../saves/" + scenarioName + ".sco";

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                for (int i = 0; i < all; ++i)
                {
                    sw.WriteLine(sce[i].desc);
                    sw.WriteLine(sce[i].left);
                    sw.WriteLine(sce[i].right);
                    sw.WriteLine(sce[i].fight);
                    sw.WriteLine(sce[i].search);
                }
            }
        }

        public void updateList(int i, mission s)
        {
            sce[actMission] = s;
            if (i < all)
            {
                actMission = i;
            }
            else
            {
                mission seged;
                seged.desc = "";
                seged.left = "";
                seged.right = "";
                seged.fight = all.ToString();
                seged.search = all.ToString();
                sce.Add(seged);
                actMission = all;
                ++all;
            }
        }

        public void toMission(string str, mission s)
        {
            mission seged;
            seged.desc = "";
            seged.left = "";
            seged.right = "";
            seged.fight = all.ToString();
            seged.search = all.ToString();

            switch(str)
            {
                case "left":
                    if (s.left == "")
                    {
                        s.left = all.ToString();
                        sce[actMission] = s;
                        actMission = Convert.ToInt32(s.left);
                        sce.Add(seged);
                        ++all;
                    }
                    else
                    {
                        updateList(Convert.ToInt32(s.left), s);
                    }
                    break;
                case "right":
                    if (s.right == "")
                    {
                        s.right = all.ToString();
                        sce[actMission] = s;
                        actMission = Convert.ToInt32(s.right);
                        sce.Add(seged);
                        ++all;
                    }
                    else
                    {
                        updateList(Convert.ToInt32(s.right), s);
                    }
                    break;
                case "fight":
                    if (s.fight == "")
                    {
                        s.fight = all.ToString();
                        sce[actMission] = s;
                        actMission = Convert.ToInt32(s.fight);
                        seged.desc = "FIGHT:";
                        sce.Add(seged);
                        ++all;
                    }
                    else
                    {
                        updateList(Convert.ToInt32(s.fight), s);
                    }
                    break;
                case "search":
                    if (s.search == "")
                    {
                        s.search = all.ToString();
                        sce[actMission] = s;
                        actMission = Convert.ToInt32(s.search);
                        seged.desc = "PUZZLE:";
                        sce.Add(seged);
                        ++all;
                    }
                    else
                    {
                        updateList(Convert.ToInt32(s.search), s);
                    }
                    break;
            }
        }

        public mission getActMission()
        {
            return sce[actMission];
        }

        public int getActMissionNum()
        {
            return actMission;
        }

        public string getName()
        {
            return scenarioName;
        }

        public int getAllNum()
        {
            return all-1;
        }

    }
}
